package com.beikbank.service.impl;

import com.beikbank.bean.qo.UserListQO;
import com.beikbank.bean.qo.UserLoginQO;
import com.beikbank.bean.vo.OnlineListVO;
import com.beikbank.bean.vo.OnlineVO;
import com.beikbank.bean.vo.UserListVO;
import com.beikbank.bean.vo.UserLoginVO;
import com.beikbank.dao.BKUserDao;
import com.beikbank.service.BKUserService;
import org.apache.log4j.Logger;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangzhanjin on 2018/1/16.
 */
@Service
public class BKUserServiceImpl implements BKUserService {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private BKUserDao bkUserDao;


    /**
     * 用户控制查询返回
     * @param userListQO
     * @return
     */
    @Override
    public  List<UserListVO> userListSearch(UserListQO userListQO){
        List<UserListVO> list = null;
        try {
            list = bkUserDao.userListSearch(userListQO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list==null?null:list;
    }


    /**
     * 用户日志查询返回
     * @param userLoginQO
     * @return
     */
    @Override
    public List<UserLoginVO> userLogSearch(UserLoginQO userLoginQO){
        List<UserLoginVO> list = null;
        try {
            list = bkUserDao.userLogSearch(userLoginQO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list==null?null:list;
    }


    /**
     * 用户锁定
     * @param userCode
     * @return
     */
    @Override
    public Integer updateUserLock(String userCode){
        Integer integer = 0;
        try {
            integer = bkUserDao.updateUserLock(userCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return integer;
    }


    /**
     * 用户在线数统计
     * @param dateType
     * @param timeEnd
     * @return
     */
    @Override
    public OnlineListVO getOnlineSearch(String dateType, String timeEnd){
        OnlineListVO onlineListVO = new OnlineListVO();
        String[] createTimeArray = new String[20];
        String[] numArray = new String[20];
        List<OnlineVO> list = null;
        try {
            Map<String, String> map = new HashMap<>();
            map.put("timeEnd", timeEnd);
            if("1".equals(dateType)){
                list = bkUserDao.getOnlineMonthSearch(map);
            }else{
                list = bkUserDao.getOnlineSearch(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list!=null&&list.size()>0){
            OnlineVO vo;
            for(int i=0,total = list.size()-1; i<= total; i++){
                vo = list.get(total-i);
                createTimeArray[i] = vo.getCreateTime();
                numArray[i] = vo.getNum();
            }

            onlineListVO.setCreateTimeArray(createTimeArray);
            onlineListVO.setNumArray(numArray);
        }
        return onlineListVO;
    }



}
